<?php
namespace app\article\model;
use app\common\lib\Paginationm;
use app\common\lib\Paginations;
use think\Db;
use think\Controller;

class ArticlesExtend extends Controller
{
    protected $beforeActionList = ['start'];
    public function start() {
        $this->itemType = 'article';
        $this->itemName = '内容';
        $this->item = 'Articles';
        $this->itemCategory = 'ArticlesCategory';
        $this->itemContent = 'ArticlesContent';
        $this->itemSearch = 'ArticlesSearch';
        $this->itemSearchRel = 'ArticlesSearchRel';
        $this->itemModelNameSpace = 'app\article\model\Articles';
        $this->itemCategoryModelNameSpace = 'app\article\model\ArticlesCategory';
    }
    
	public function getItemSearchList($paramData)
    {
        $limit = $paramData['limit'] ? $paramData['limit'] : 10;
        $page = $paramData['page'] ? $paramData['page'] : 1;
        $wordsArray = getWords($paramData['keywords']);
        $searchArray = db('ArticlesSearch')->field('id')->where('name','in',$wordsArray)->select();
        $searchArray = array_column($searchArray,'id');
        $searchRelList = db('ArticlesSearchRel')->where('search_id','in',$searchArray)->select();
        $searchRelArray = array_column($searchRelList,'item_id');
        $searchRelArray = array_filter($searchRelArray);
        $searchRelArray = array_count_values($searchRelArray);
        arsort($searchRelArray);
        $searchRelArray = array_keys($searchRelArray);
        if ($searchRelArray) {
            $allPageNum = ceil(count($searchRelArray) / $limit);
            if ($page <= $allPageNum) {
                $searchRelArray = array_slice($searchRelArray,($page - 1) * $limit,$limit);
            }
            if ($searchRelArray) {
                foreach ($searchRelArray as $key => $value) {
                    $itemList[] = model('app\article\model\Articles')->getItemInfo($value);
                }
            }
        }
        return $itemList;
    }
    public function getItemSearchPagination($paramData)
    {
        $limit = $paramData['limit'] ? $paramData['limit'] : 10;
        $page = $paramData['page'] ? $paramData['page'] : 1;
        $wordsArray = getWords($paramData['keywords']);
        $searchArray = db('ArticlesSearch')->field('id')->where('name','in',$wordsArray)->select();
        $searchArray = array_column($searchArray,'id');
        $searchRelList = db('ArticlesSearchRel')->where('search_id','in',$searchArray)->select();
        $searchRelArray = array_column($searchRelList,'item_id');
        $searchRelArray = array_filter($searchRelArray);
        $searchRelArray = array_count_values($searchRelArray);
        arsort($searchRelArray);
        $searchRelArray = array_keys($searchRelArray);
        $count = count($searchRelArray);
        
        $baseUrl = $this->domain . '/search?q=' . $paramData['keywords'];
        $pagination_array = array(
            'base_url' => $baseUrl,
            'total_rows' => $count,
            'per_page' => $limit,
            'cur_page' => $page,
            'page_break' => '='
        );
        $pagination = new Paginations($pagination_array);
        $pagination = $pagination->create_links();
        
        return $pagination;
    }
    
	public function getItemRandList($paramData)
    {
        $cid = $paramData['cid'] ? $paramData['cid'] : null;
        $limit = $paramData['limit'] ? $paramData['limit'] : 10;
        if ($cid == '' || $cid == null) {
            $itemFieldList = db($this->item)->orderRaw('rand()')->limit($limit)->select();
        } else {
        	$data['pid'] = $cid;
            $itemCategoryList = model($this->itemCategoryModelNameSpace)->getCategory($data);
            if ($itemCategoryList) {
                foreach ($itemCategoryList as $key => $value) {
                    $cids[] = $value['id'];
                }
            }
            if ($itemCategoryList) {
                $itemFieldList = db($this->item)->orderRaw('rand()')->whereOr('cid',$cid)->limit($limit)->whereOr('cid','in',$cids)->select();
            } else {
                $itemFieldList = db($this->item)->orderRaw('rand()')->where('cid',$cid)->limit($limit)->select();
            }
        }
        if ($itemFieldList) {
            foreach($itemFieldList as $k => $v) {
                $itemList[] = model($this->itemModelNameSpace)->getItemInfo($v['id'],'',$v);
            }
        }
        return $itemList;
    }
    
	public function getItemAboutList($paramData)
    {
        $cid = $paramData['cid'] ? $paramData['cid'] : null;
        $limit = $paramData['limit'] ? $paramData['limit'] : 10;
        $itemId = $paramData['itemId'] ? $paramData['itemId'] : '';
        $itemIdArray = explode(',',$itemId);
        $tagsRelList = db('TagsRel')->field('tags_id')->where('item_id','in',$itemIdArray)->select();
        $tempTagsArray = [];
        if ($tagsRelList) {
        	foreach ($tagsRelList as $key => $val) {
        		$tempTagsArray[] = $val['tags_id'];
        	}
        	$itemFieldList = db('TagsRel')->field('item_id')->where('tags_id','in',$tempTagsArray)->Distinct(true)->limit($limit)->order('id','desc')->select();
        }
        if ($itemFieldList) {
            foreach($itemFieldList as $k => $v) {
            	$itemList[] = model($this->itemModelNameSpace)->getItemInfo('',$v['item_id']);
            }
        }
        return $itemList;
    }
    
	public function getItemLoveList($paramData)
    {
        $cid = $paramData['cid'] ? $paramData['cid'] : null;
        $limit = $paramData['limit'] ? $paramData['limit'] : 10;
        $page = $paramData['page'] ? $paramData['page'] : 1;
        $itemId = $paramData['itemId'] ? $paramData['itemId'] : '';
        $orderBy = $paramData['orderBy'] ? $paramData['orderBy'] : 'publish_time';
        $order = $paramData['order'] ? $paramData['order'] : 'desc';
        
        $itemIdArray = explode(',',$itemId);
        $tagsRelList = db('TagsRel')->field('tags_id')->where('item_id','in',$itemIdArray)->select();
        $tempTagsArray = [];
        if ($tagsRelList) {
        	foreach ($tagsRelList as $key => $val) {
        		$tempTagsArray[] = $val['tags_id'];
        	}
        	$itemFieldList = db('Tags')->field('title')->where('id','in',$tempTagsArray)->Distinct(true)->order('id','desc')->select();
        }
        if ($itemFieldList) {
            foreach ($itemFieldList as $key => $val) {
                if ($val) {
                    $sq[] = "%".$val['title']."%";
                }
            }
            $keywordsWhere['title']  = ['like',$sq,'OR'];
        }
    	$itemList = db($this->item)->where($keywordsWhere)->Distinct(true)->limit($limit)->order($orderBy,$order)->select();
        if ($itemList) {
            foreach($itemList as $k => $v) {
                $itemList[$k] = model($this->itemModelNameSpace)->getItemInfo($v['id'],'',$v);
            }
        }
        return $itemList;
    }
    
    public function getItemWeekList($paramData)
    {
        $cid = $paramData['cid'] ? $paramData['cid'] : null;
        $limit = $paramData['limit'] ? $paramData['limit'] : 10;
        $orderBy = $paramData['orderBy'] ? $paramData['orderBy'] : 'views';
        $order = $paramData['order'] ? $paramData['order'] : 'desc';
       	$todyStartTime = time();
        if ($cid == '' || $cid == null) {
            $itemFieldList = db($this->item)->where('publish_time','between',[$todyStartTime - (7 * 24 * 60 * 60) ,$todyStartTime])->limit($limit)->order($orderBy,$order)->select();
        } else {
        	$data['pid'] = $cid;
            $itemCategoryList = model($this->itemCategoryModelNameSpace)->getCategory($data);
            if ($itemCategoryList) {
                foreach ($itemCategoryList as $key => $value) {
                    $cids[] = $value['id'];
                }
            }
            if ($itemCategoryList) {
                $itemFieldList = db($this->item)->where('publish_time','between',[$todyStartTime - (7 * 24 * 60 * 60) ,$todyStartTime])->whereOr('cid',$cid)->whereOr('cid','in',$cids)->limit($limit)->order($orderBy,$order)->select();
            } else {
                $itemFieldList = db($this->item)->where('publish_time','between',[$todyStartTime - (7 * 24 * 60 * 60) ,$todyStartTime])->where('cid',$cid)->order($orderBy,$order)->limit($limit)->select();
            }
        }
        if ($itemFieldList) {
            foreach($itemFieldList as $k => $v) {
                $itemList[] = model($this->itemModelNameSpace)->getItemInfo($v['id'],'',$v);
            }
        }
        return $itemList;
    }
    
    public function getItemMonthList($paramData)
    {
        $cid = $paramData['cid'] ? $paramData['cid'] : null;
        $limit = $paramData['limit'] ? $paramData['limit'] : 10;
        $orderBy = $paramData['orderBy'] ? $paramData['orderBy'] : 'views';
        $order = $paramData['order'] ? $paramData['order'] : 'desc';
       	$todyStartTime = time();
        if ($cid == '' || $cid == null) {
            $itemFieldList = db($this->item)->where('publish_time','between',[$todyStartTime - (30 * 24 * 60 * 60) ,$todyStartTime])->limit($limit)->order($orderBy,$order)->select();
        } else {
        	$data['pid'] = $cid;
            $itemCategoryList = model($this->itemCategoryModelNameSpace)->getCategory($data);
            if ($itemCategoryList) {
                foreach ($itemCategoryList as $key => $value) {
                    $cids[] = $value['id'];
                }
            }
            if ($itemCategoryList) {
                $itemFieldList = db($this->item)->where('publish_time','between',[$todyStartTime - (30 * 24 * 60 * 60) ,$todyStartTime])->whereOr('cid',$cid)->whereOr('cid','in',$cids)->limit($limit)->order($orderBy,$order)->select();
            } else {
                $itemFieldList = db($this->item)->where('publish_time','between',[$todyStartTime - (30 * 24 * 60 * 60) ,$todyStartTime])->where('cid',$cid)->order($orderBy,$order)->limit($limit)->select();
            }
        }
        if ($itemFieldList) {
            foreach($itemFieldList as $k => $v) {
                $itemList[] = model($this->itemModelNameSpace)->getItemInfo($v['id'],'',$v);
            }
        }
        return $itemList;
    }
    
}